<template>
	<view>
		<!--    临时工人使用申请-->
		<HeaderNavTabarSvg :title="title">
			<image class="bukaSvg" src="/static/icon/auditSVG/buka.svg" @tap="goHistoryView"></image>
		</HeaderNavTabarSvg>

		<view class="measurement-info-list">
			<text class="info-list-left isrequired">项目名称(工单信息)</text>
			<view class="info-list-right">
				<view class="info-content">
					<input v-model="ItemRequisitionList.projectName" adjust-position class="uni-input" placeholder="请输入"
						type="text" />
				</view>
			</view>
		</view>
		<!--    <view class="measurement-info-list">
      <text class="info-list-left isrequired">申请人</text>
      <view class="info-list-right">
        <view class="info-content">
          <input
            v-model="ItemRequisitionList.applicant"
            adjust-position
            class="uni-input"
            placeholder="请输入"
            type="text"
          />
        </view>
      </view>
    </view> -->
		<view class="picker-content" @tap="escortsFn">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">申请人</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.escorts.length > 0" v-for="(item,index) in ItemRequisitionList.escorts"
						:key="index" class="uni-input">{{ item.nickName }}
						&nbsp
					</view>
					<view v-else class="uni-inputs">请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<view class="picker-content" @tap="toggle">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">申请日期</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.date" class="uni-input">{{ ItemRequisitionList.date }}
					</view>
					<view v-else class="uni-inputs">请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="datetimePicker" v-model="date" :formatter="formatter" mode="date" @confirm="confirm">
		</uv-datetime-picker>
		<view class="picker-content" @tap="toggle2">
			<view class="measurement-info-list">
				<text class="info-list-left isrequired">预支日期</text>
				<view class="info-list-right">
					<view v-if="ItemRequisitionList.date2" class="uni-input">{{ ItemRequisitionList.date2 }}
					</view>
					<view v-else class="uni-inputs">请选择</view>
					<i class="iconfont icon-youjiantou"></i>
				</view>
			</view>
		</view>
		<uv-datetime-picker ref="datetimePicker2" :min-date="ItemRequisitionList.date" v-model="date2"
			:formatter="formatter" mode="date" @confirm="confirm2">
		</uv-datetime-picker>
		<view class="measurement-info-list">
			<text class="info-list-left isrequired">使用临时施工队的原因</text>
			<view class="info-list-right">
				<view class="info-content">
					<input v-model="ItemRequisitionList.reason" adjust-position class="uni-input" placeholder="请输入" type="text" />
				</view>
			</view>
		</view>
		<view class="measurement-info-list">
			<text class="info-list-left isrequired">项目区域</text>
			<view class="info-list-right">
				<view class="info-content">
					<input v-model="ItemRequisitionList.projectArea" adjust-position class="uni-input" placeholder="请输入"
						type="text" />
				</view>
			</view>
		</view>
		<view class="measurement-info-list">
			<text class="info-list-left isrequired">预计工费总金额</text>
			<view class="info-list-right">
				<view class="info-content">
					<input v-model="ItemRequisitionList.estimatedCost" adjust-position class="uni-input" placeholder="请输入"
						type="text" />
				</view>
			</view>
		</view>
		<view class="measurement-info-list">
			<text class="info-list-left isrequired">拟使用的施工队的工长名称及电话</text>
			<view class="info-list-right">
				<view class="info-content">
					<input v-model="ItemRequisitionList.leaderAndPhone" adjust-position class="uni-input" placeholder="请输入"
						type="text" />
				</view>
			</view>
		</view>
		<!--    说明附件-->
		<DescriptionAttachment />
		<!--    审批流程-->
		<AuditApplicationSteps :ruleId="'linshigong'" />
		<!--    底部占位符-->
		<view class="blank"></view>
		<!--    保存-->
		<view class="buttonContainer">
			<button class="buttonClass" @click="clickSave">提交</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import HeaderNavTabarSvg from '@/components/HeaderNavTabarSvg/HeaderNavTabar.vue'
	import AuditApplicationSteps from '@/components/audit/function/auditApplicationSteps.vue'
	import DescriptionAttachment from '@/components/audit/function/descriptionAttachment.vue'
	import optionDeptPerson from '@/store/modules/optionDeptPerson'
	import {
		ref
	} from 'vue'
	import dayjs from 'dayjs'
	import {
		isObjectValuesNotEmpty
	} from '@/utils/objIsnull.js'

	const title = '临时工人使用申请'

	const popup = ref(null)
	const datetimePicker = ref(null)
	const popup2 = ref(null)
	const datetimePicker2 = ref(null)
	let date = dayjs()
	let date2 = dayjs()

	const ItemRequisitionList = ref({
		date: '',
		date2: '',
		projectName: '',
		escorts: [],
		reason: '',
		projectArea: '',
		estimatedCost: '',
		leaderAndPhone: '',
	})

	// 打开选择器
	const toggle = (type) => {
		datetimePicker.value.open()
	}

	// 打开选择器
	const toggle2 = (type) => {
		if (ItemRequisitionList.value.date) {
			datetimePicker2.value.open()
		} else {
			uni.showToast({
				title: '请先选择申请日期',
				icon: 'none'
			})
		}
	}

	const confirm = (obj) => {
		ItemRequisitionList.value.date = dayjs(obj.value).format('YYYY-MM-DD')
	}

	const confirm2 = (obj) => {
		ItemRequisitionList.value.date2 = dayjs(obj.value).format('YYYY-MM-DD')
	}

	const escortsFn = () => {
		uni.navigateTo({
			url: '/components/OptionDeptPerson/index?type=person',
		})
	}

	const formatter = (type, value) => {
		if (type === 'year') {
			return `${value}年`
		}
		if (type === 'month') {
			return `${value}月`
		}
		if (type === 'day') {
			return `${value}日`
		}
		return value
	}

	let attchment = ref([])
	const getDesignDrawingsEmit = (emit) => {
		attchment.value = emit
	}

	const clickSave = () => {
		if (isObjectValuesNotEmpty(ItemRequisitionList.value)) {
			let params = {
				ruleId: 'linshigong',
				type: '临时工人使用申请',
				attchment: JSON.stringify(attchment.value),
				userId: userId.value,
				approve: approvalProcessState.state.approvalProcess,
				projectName: ItemRequisitionList.value.projectName,
				date: ItemRequisitionList.value.date,
				date2: ItemRequisitionList.value.date2,
				personnel: JSON.stringify(ItemRequisitionList.value.escorts),
				reason: ItemRequisitionList.value.reason,
				projectArea: ItemRequisitionList.value.projectArea,
				estimatedCost: ItemRequisitionList.value.estimatedCost,
				leaderAndPhone: ItemRequisitionList.value.leaderAndPhone,
			}
			console.log(params, '临时工人申请申请提交params')
			// let { code } = await addAdministrativeApply(params)
			// if (code === 200) {
			//   uni.showToast({
			//     title: '提交成功',
			//     duration: 1500,
			//     icon: 'success',
			//   })
			//   setTimeout(() => {
			//     uni.navigateBack()
			//   }, 1000)
			// }
		} else {
			uni.showToast({
				title: '请输入完整数据',
				icon: 'error'
			})
		}

	}
	const goHistoryView = () => {
		uni.navigateTo({
			url: '/components/TypeDefineModel?type=临时工人使用申请&apiName=其他',
		})
	}
	onShow(() => {
		if (optionDeptPerson.state.optionPerson) {
			ItemRequisitionList.value.escorts = optionDeptPerson.state.optionPerson.map((v) => {
				return {
					userId: v.userId,
					nickName: v.nickName,
				}
			})
		}
	})
	onLoad(() => {
		// 历史打卡记录
		// const params = {
		//   userId: profile.value.userId,
		//   deptId: profile.value.deptId,
		//   punchTimeLte: new dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
		//   punchTimeLte: new dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
		// }
		//
		// attendanceRecordList.value = (await getAttendanceRecordList(params)).rows
	})
</script>

<style lang="scss" scoped>
	@import '@/static/style/index.scss';

	.bukaSvg {
		width: 32rpx;
		height: 32rpx;
	}

	.buttonContainer {
		position: fixed;
		bottom: 0;
		display: flex;
		justify-content: center;
		width: 100%;
		align-items: center;
		padding: 16rpx;
		background-color: #fff;
	}

	.buttonClass {
		display: flex;
		width: 686rpx;
		height: 72rpx;
		justify-content: center;
		align-items: center;
		background: #165dff;
		border-radius: 16rpx;
		color: #fff;
		font-size: 32rpx;
		//margin-top: 40rpx;
		overflow: hidden;
	}

	.blank {
		height: calc(72rpx + 32rpx + 20rpx);
	}

	.bukabanci {
		.info-content {
			color: #86909c !important;
		}

		.info-list-left {
			// @include font-color-auxiliary;
			color: #86909c !important;
		}
	}

	.uni-inputs {
		color: #86909c !important;
	}

	.textarea-box {
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.textinfo-list {
			margin-top: 20rpx;
			width: 100%;
			height: 200rpx;
		}
	}

	.popup-content {
		background: #f7f8fa;

		.header-box {
			padding: 30rpx 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			p {
				color: #1d2129;
				font-size: 34rpx;
				font-weight: 500;
			}

			.module-image-ba {
				border-radius: 32rpx;
				background: #f2f3f5;
				width: 45rpx;
				height: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				image {
					width: 32rpx;
					height: 32rpx;
				}
			}
		}

		.card-list {
			display: flex;
			align-items: center;
			flex-direction: column;
			padding: 20rpx 30rpx;
			box-sizing: border-box;

			.list {
				box-shadow: 0 4rpx 16rpx 0 #0000000d;
				box-sizing: border-box;
				width: 100%;
				background: #fff;
				padding: 16rpx 24rpx;
				border-radius: 8rpx;
				margin-bottom: 10rpx;

				h4 {
					color: #1d2129;
					font-size: 32rpx;
					font-weight: 400;
				}

				p {
					color: #86909c;
					font-size: 24rpx;
					font-weight: 400;
					margin-top: 10rpx;
				}
			}
		}
	}

	.measurement-info-list {
		@include flex($align: center, $justify: space-between);
		padding: $spacing-row-base;
		background: #fff;
		margin-top: 16rpx;

		.info-list-left {
			// @include font-color-auxiliary;
			color: #1d2129;
			font-size: $font-size-base;
		}

		.info-list-right {
			@include flex($align: center);

			.info-content {
				@include textStyleBase;
				font-size: 34rpx;
			}

			.iconfont {
				margin-left: 16rpx;
				// @include font-color-auxiliary;
				color: $uni-fc-fu-lin;
			}
		}

		.unit {
			// @include font-color-auxiliary;
			color: $uni-fc-fu-lin;
		}
	}

	:deep(.uni-input) {
		text-align: right;
	}
</style>